﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CodeMapMgnt.ascx.cs" Inherits="Controls_CodeMapMgnt" %>
<script runat="server">
    /* list */
    protected void gv_DataBinding(object sender, EventArgs e)
    {
        var sql = new StringBuilder();
        var dbparams = new List<DBParameter>();

        sql.AppendFormat(string.Format(@"SELECT cm.* FROM CodeMap cm WHERE TableName = @TableName AND ColName = @ColName "));

        dbparams.Add(new DBParameter("TableName", TableName));
        dbparams.Add(new DBParameter("ColName", ColumnName));

        if (txtValue.Text.Length > 0)
        {
            sql.AppendFormat("AND Value=@Value");
            dbparams.Add(new DBParameter("Value", txtValue.Text));
        }
        
        if (txtText.Text.Length > 0)
        {
            sql.AppendFormat("AND {0}=@Text", LocManager.Current.GetTextCol("CodeMap", "Value"));
            dbparams.Add(new DBParameter("Text", txtText.Text));
        }
        
        var db = new DB();
        var dt = db.Execute(sql.ToString(), dbparams.ToArray());

        (sender as DataBoundControl).DataSource = dt;

    }

    protected void ddlStatus_DataBinding(object sender, EventArgs e)
    {
        (sender as ListControl).DataValueField = "Value";
        (sender as ListControl).DataTextField = "Text";
        (sender as ListControl).DataSource = CodeManager.Current.GetData("CodeMap", "Status");
    }
    /* end of list */
</script>
<asp:PlaceHolder ID="plQuery" runat="server">
    <table>
        <tr>
            <td class="label">
                <asp:Localize ID="Localize1" runat="server" Text="<%$Resources: Value %>"></asp:Localize>
            </td>
            <td class="text">
                <asp:TextBox ID="txtValue" runat="server" Text='<%# string.Empty %>'></asp:TextBox>
            </td>
            <td class="label">
                <asp:Localize ID="Localize2" runat="server" Text="<%$Resources: Text %>"></asp:Localize>
            </td>
            <td class="text">
                <asp:TextBox ID="txtText" runat="server" Text='<%# string.Empty %>'></asp:TextBox>
            </td>
            <td class="text">
                <asp:Button ID="btnAdd" runat="server" Text="<%$Resources: Form, btnAdd %>" OnClick="btnAdd_Click" />
                <ajax:ConfirmButtonExtender ID="cbAdd" runat="server" TargetControlID="btnAdd" ConfirmText="<%$Resources: Form, cb %>">
                </ajax:ConfirmButtonExtender>
                <asp:Button ID="btnQuery" runat="server" Text="<%$Resources: Form, btnQuery %>" OnClick="btnQuery_Click" />
            </td>
        </tr>
    </table>
</asp:PlaceHolder>
<My:MyGridView ID="gv" runat="server" OnDataBinding="gv_DataBinding" OnRowCommand="gv_ItemCommand"
    DataKeyNames="TableName, ColName, Value">
    <Columns>
        <asp:TemplateField HeaderText="<%$ Resources: Value %>">
            <ItemTemplate>
                <%# Eval("Value")%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources: Text %>">
            <ItemTemplate>
                <asp:TextBox ID="txtText" runat="server" Text='<%# CodeManager.Current.GetText(TableName, ColumnName, Eval("Value")) %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <My:MyDropDownList ID="ddlStatus" runat="server" OnDataBinding="ddlStatus_DataBinding"
                    Text='<%# Eval("Status") %>'>
                </My:MyDropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</My:MyGridView>
<p>
</p>
<div style="text-align: center">
    <asp:Button ID="btnSave" runat="server" Text="<%$Resources: Form, btnSave %>" OnClick="btnSave_Click" />
    <asp:Button ID="btnCancel" runat="server" Text="<%$Resources: Form, btnCancel %>" />
</div>
